package com.duck.duckchatbackend.controller;
import java.io.IOException;
import java.util.List;

import com.duck.duckchatbackend.annotation.GlobalInterceptor;
import com.duck.duckchatbackend.entity.constans.Constants;
import com.duck.duckchatbackend.entity.dto.TokenUserInfoDto;
import com.duck.duckchatbackend.entity.query.UserInfoQuery;
import com.duck.duckchatbackend.entity.po.UserInfo;
import com.duck.duckchatbackend.entity.vo.ResponseVO;
import com.duck.duckchatbackend.entity.vo.UserInfoVO;
import com.duck.duckchatbackend.exception.BusinessException;
import com.duck.duckchatbackend.service.UserInfoService;
import com.duck.duckchatbackend.utils.CopyTools;
import com.duck.duckchatbackend.utils.StringTools;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;

/**
 * 用户信息 Controller
 */
@RestController("userInfoController")
@RequestMapping("/userInfo")
public class UserInfoController extends ABaseController{
	@Resource
	private UserInfoService userInfoService;
	/**
	 * 获取用户信息
	 * 该方法通过请求中的信息获取用户详情，并返回给前端
	 * 主要涉及用户身份验证和用户信息的转换与封装
	 *
	 * @param request HTTP请求对象，用于获取请求中的Token信息
	 * @return 返回一个包含用户信息的ResponseVO对象
	 */
	@RequestMapping("/getUserInfo")
	@GlobalInterceptor
	public ResponseVO getUserInfo(HttpServletRequest request) {
	    // 从请求中获取Token用户信息
	    TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
	    // 根据用户ID获取用户信息
	    UserInfo userInfo = userInfoService.getUserInfoByUserId(tokenUserInfoDto.getUserId());
	    // 将UserInfo对象转换为UserInfoVO对象
	    UserInfoVO userInfoVO = CopyTools.copy(userInfo, UserInfoVO.class);
	    // 设置用户是否为管理员
	    userInfoVO.setAdmin(tokenUserInfoDto.getAdmin());
	    // 返回包含用户信息的成功响应
	    return getSuccessResponseVO(userInfoVO);
	}


	@RequestMapping("/saveUserInfo")
	@GlobalInterceptor
	public ResponseVO saveUserInfo(HttpServletRequest request,
								    UserInfo userInfo,
								   MultipartFile avatarFile,
								   MultipartFile avatarCover) throws IOException {
		TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
		userInfo.setUserId(tokenUserInfoDto.getUserId());
		userInfo.setPassword(null);
		userInfo.setStatus(null);
		userInfo.setCreateTime(null);
		userInfo.setLastLoginTime(null);
		this.userInfoService.updateUserInfo(userInfo,avatarFile,avatarCover);
		return getUserInfo(request);
	}

	@RequestMapping("/updatePassword")
	@GlobalInterceptor
	public ResponseVO updatePassword(HttpServletRequest request,
									 @NotEmpty @Pattern(regexp = Constants.REGEX_PASSWORD) String password, String newPassword) throws IOException {
		TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
        UserInfo userInfo = new UserInfo();
		userInfo.setPassword(StringTools.encodeByMD5(newPassword));
		this.userInfoService.updateUserInfoByUserId(userInfo,tokenUserInfoDto.getUserId());
		//TODO 强制退出，重新登录
		return getSuccessResponseVO(null);
	}
    @RequestMapping("/logout")
	@GlobalInterceptor
	public ResponseVO logout(HttpServletRequest request) {
		TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo(request);
		//TODO 强制退出，重新登录
		return getSuccessResponseVO(null);
	}
}